package cn.lg.soar.system.biz.modules.auth.client;

import cn.lg.soar.core.annotations.ApiClient;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.enums.OrgTypeEnum;
import cn.lg.soar.system.api.model.OrgDTO;
import cn.lg.soar.system.api.service.IOrgApi;
import cn.lg.soar.system.biz.modules.auth.entity.Org;
import cn.lg.soar.system.biz.modules.auth.service.IOrgService;
import cn.lg.soar.system.biz.modules.auth.service.IPostService;
import cn.lg.soar.system.biz.modules.auth.service.UserPostRelService;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luguoxiang
 * @date 2022/1/3
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Hidden
@ApiClient("boot")
@RestController
@RequestMapping(ModuleConst.CLIENT_PATH + "/org")
public class OrgApiClient implements IOrgApi {

    @Autowired
    private IOrgService service;
    @Autowired
    private IPostService postService;
    @Autowired
    private UserPostRelService userOrgService;

    @GetMapping("getById")
    @Override
    public OrgDTO getById(@RequestParam(name = "id") Long id) {
        return toOrgDTO(service.getById(id));
    }

    @GetMapping("listByIds")
    @Override
    public List<OrgDTO> listByIds(@RequestParam(name = "ids") List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return service.listByIds(ids).stream().map(this::toOrgDTO).collect(Collectors.toList());
    }

    @GetMapping("ancestorLink")
    @Override
    public List<OrgDTO> ancestorLink(@RequestParam(name = "id") Long id) {
        List<Org> list = service.getAncestorById(id);
        return list.stream().map(this::toOrgDTO).collect(Collectors.toList());
    }

    @GetMapping("idsByUserId")
    @Override
    public List<Long> idsByUserId(@RequestParam(name = "userId") Long userId) {
        return userOrgService.getOrgIds(userId);
    }

    @GetMapping("listByUserId")
    @Override
    public List<OrgDTO> listByUserId(@RequestParam("userId") Long userId) {
        return service.listByUserId(userId);
    }

    @GetMapping("listByUserIdByType")
    @Override
    public List<OrgDTO> listByUserId(@RequestParam("userId") Long userId, @RequestParam("type") OrgTypeEnum type) {
        return service.listByUserId(userId, type);
    }

    @GetMapping("getUserOrg")
    @Override
    public OrgDTO getUserOrg(@RequestParam("userId") Long userId) {
        return service.getUserOrg(userId);
    }

    @GetMapping("getUserOrgByType")
    @Override
    public OrgDTO getUserOrg(@RequestParam("userId") Long userId, @RequestParam("type") OrgTypeEnum type) {
        return service.getUserOrg(userId, type);
    }

    @GetMapping("getUserOrgByLevel")
    @Override
    public OrgDTO getUserOrg(@RequestParam("userId") Long userId, @RequestParam("level") int level) {
        return service.getUserOrg(userId, level);
    }

    private OrgDTO toOrgDTO(Org org) {
        OrgDTO orgDTO = new OrgDTO();
        orgDTO.setId(org.getId());
        orgDTO.setName(org.getName());
        orgDTO.setAlias(org.getAlias());
        orgDTO.setRemark(org.getRemark());
        orgDTO.setParentId(org.getParentId());
        orgDTO.setSort(org.getSort());
        orgDTO.setType(org.getType());
        orgDTO.setDisabled(org.getDisabled());
        return orgDTO;
    }
}
